<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="../../../include/meta.jsp"%>

<title>通用权限管理系统</title>
<%@include file="../../../include/css.jsp"%>
</head>
<body>
	<%@include file="../include/header.jsp"%>
	<nav aria-label="breadcrumb">
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a
				href="${pageContext.request.contextPath}/app/main.html"><i
					class="fas fa-home"></i>主页</a></li>
			<li class="breadcrumb-item"><a
				href="${pageContext.request.contextPath}/app/role/list.html">角色管理</a></li>
			<li class="breadcrumb-item active" aria-current="page">编辑角色</li>
		</ol>
	</nav>

	<div class="container">
		<div class="row">
			<div class="col-12">
				<form
					action="${pageContext.request.contextPath}/app/role/update.html"
					method="post">
					<input type="hidden" name="id" value="${role.id}"> <input
						type="hidden" name="page" value="${page}"> <input
						type="hidden" name="size" value="${size}">
					<div class="form-group row">
						<label for="">角色名称</label> <input type="text" class="form-control"
							id="name" name="name" value="${role.name}">
					</div>

					<div class="form-group row">
						<label for="">角色授权</label>
					</div>

					<c:forEach items="${functions}" var="f">
						<div class="form-group row">
							<div class="form-group form-check">
								<input type="checkbox" class="form-check-input"
									id="fg${f.functionGroup.id}" name="functions"
									value="${f.functionGroup.id}" ${f.functionGroup.checked?"checked":""}
									onchange="choose('${f.functionGroup.id}')"> <label
									class="form-check-label" for="fg${f.functionGroup.id}">${f.functionGroup.name}</label>
							</div>
						</div>

						<div id="d${f.functionGroup.id}" class="form-group row"
							style="padding-left: 20px; border-bottom: 1px solid #ccc;">
							<c:forEach items="${f.functionalModules}" var="fm">
								<div class="form-group form-check col-md-2">
									<input type="checkbox" class="form-check-input" id="fm${fm.id}"
										name="functions" value="${fm.id}"
										onchange="chooseGroup('${f.functionGroup.id}')"  ${fm.checked?"checked":""}> <label
										class="form-check-label" for="fm${fm.id}">${fm.name}</label>
								</div>
							</c:forEach>
						</div>
					</c:forEach>

					<div class="form-group row">
						<div class="col-md-4">
							<button type="submit" class="btn btn-primary btn-block">保存</button>
						</div>
						<div class="col-md-3">
							<button type="reset" class="btn btn-warning btn-block">重置</button>
						</div>
						<div class="col-md-2 offset-md-3">
							<a class="btn btn-secondary btn-block"
								href="${pageContext.request.contextPath}/app/role/list.html?page=${page}&size=${size}">返回</a>
						</div>
					</div>
				</form>
				<div id="message" class="lead text-danger" style="margin-top: 10px;">${message}
					<c:if test="${message!=null}">
						<script>
							setTimeout(function() {
								$("#message").text("");
							}, 3000);
						</script>
					</c:if>
				</div>
			</div>
		</div>
	</div>

	<%@include file="../../../include/js.jsp"%>
	<script type="text/javascript">
		function chooseGroup(id) {
			var isChecked = false;
			$("#d" + id + " :input[type='checkbox']").each(
					function(index, element) {
						if ($(element).prop("checked")) {
							isChecked = true;
							return;
						}
					});
			$("#fg" + id).prop("checked", isChecked);
		}
		function choose(id) {
			$("#d" + id + " :input[type='checkbox']").each(
					function(index, element) {
						$(element).prop("checked",
								$("#fg" + id).prop("checked"));
					});
		}
	</script>
</body>
</html>